// import httpURL from '../../config/httpURL'
import axios from 'axios'

const state = {
    hotMovies: [],
    timeMovies: [],
    smallMovies: [],

    yuleNews: []
};

const mutations = {
    getMovie (state, payload) {
        switch (payload.tag) {
            case 'hotMovies':
                state.hotMovies = payload.res;
                break;
            case 'timeMovies':
                state.timeMovies = payload.res;
                break;
            case 'smallMovies':
                state.smallMovies = payload.res;
                break;
            default:
                state.hotMovies = payload.res;
        }
    },

    getNews (state, payload) {
        state.yuleNews = payload.res
    }
};

const actions = {
    // 获取首页电影信息
    getMovie ({ commit }) {
        // get hotMovies
        axios.get('/dbapi/movie/in_theaters?count=6')
        .then(function (res) {
            commit({
                type: 'getMovie',
                tag: 'hotMovies',
                res: res.data.subjects
            });
        })
        .catch(function (err) {
            console.log(err);
        });

        // get timeMovies
        axios.get('/dbapi/movie/coming_soon?count=6')
        .then(function (res) {
            commit({
                type: 'getMovie',
                tag: 'timeMovies',
                res: res.data.subjects
            });
        })
        .catch(function (err) {
            console.log(err);
        });

        // get smallMovies
        axios.get('/myapi/movie/list.json?type=hot&offset=0&limit=6')
        .then(function (res) {
            commit({
                type: 'getMovie',
                tag: 'smallMovies',
                res: res.data.data.movies
            });
        })
        .catch(function (err) {
            console.log(err);
        });
    },
    // 获取首页娱乐资讯信息
    getNews ({commit}) {
        axios.get('/jhapi/toutiao/index?type=yule&key=9a6a580dc02d2b69b646d1bdea39ceeb')
        .then(function (res) {
            commit({
                type: 'getNews',
                res: res.data.result.data
            })
        })
        .catch(function (err) {
            console.log(err);
        });
    }


};

export default {
    state,
    mutations,
    actions
}